<?php

require_once(dirname(__FILE__) . '/appconf.php');

class Member extends Site {

    function setup() {

        $this->run_modes('login', 'login_imore', 'login_nmg', 'login_facebook', 'reg', 'logout_facebook', 'login_fnb', 'loginFNB', 'regTrial', 'regExpert', 'ajax_checkName', 'ajax_checkEmail', 'regFacebook', 'ajax_uploadAvatar', 'ajax_checkCaptcha', 'regSucceed', 'ajax_memberBar', 'logout', 'resendActivation', 'activation', 'forgetPassword', 'changePassword', 'ajax_checkPhone', 'MyReg', 'ajax_login_imore', 'ajax_login_fnb', "MyProfile");
        $this->start_mode('login');
        $this->selfurl = '/member.php';

        $this->key = "itrialpw01234567";
        $this->activationKey = "ImoreHK";
        $this->super_password = "@)!$2014nmg";
        parent::setup();
        include("common.php");
    }

    function cgiapp_prerun() {
        $this->Member = getModel('Model_Member');
        $this->Member->key = "itrialpw01234567";
        $this->userobj = getLibClass('UserCookies');

        $this->Lookup = getModel('Model_Lookup');
        $this->Product = getModel('Model_Product');
        $this->aes = getLibClass('AES', $this->key);
        $this->Points = getModel('Model_Points');
        $this->email_obj = getLibClass('itrialEmail');

        parent::cgiapp_prerun();
    }

    function action_logout() {

        $this->userobj->set('memberData', '');
        $this->redirect($_SERVER['HTTP_REFERER']);
    }

    function action_login() {
        $toURL = $this->set_toURL();
        $this->userobj->set('memberData', '');
        $this->tmpl_assign('selfurl', $this->selfurl);
        $this->tmpl_assign('NO_USERNAME', NO_USERNAME);
        $this->tmpl_assign('NO_PASSWORD', NO_PASSWORD);
        $this->tmpl_assign('ERR_CAPTCHA', ERR_CAPTCHA);
        $this->tmpl_assign('toURL', urlencode($toURL));
        return $this->load_tmpl('login.php');
    }

    function action_login_imore() {
        $email = $this->get_q('email', '');
        $password = $this->get_q('password', '');

        //到IMORE檢查用戶密碼是否正確
        $url = IMOREDOMAIN . "memberApi.php?action=ajax_login&username=" . urlencode($email) . "&password=" . urlencode($this->aes->encrypt($password));

        $memberData = json_decode($this->Member->go_curl($url), 1);

        if ($memberData['ret']) {
            return showMsgCode($memberData['msg'], $this->selfurl . '?action=login');
        } else {

            $this->Member->login_succeed($memberData);

            if ($password <> $this->super_password)
                $this->Points->points_record($memberData['memberInfo']['id'], 'login', "");

            $toURL = $this->s_param('toURL') ? $this->s_param('toURL') : "/";
            $this->userobj->set('toURL', '');
            $this->redirect($toURL);
        }
    }

    function action_login_fnb() {
        $this->userobj->set('memberData', '');
        if ($this->get_q('issubmit')) {

            $email = $this->get_q('email', '');
            $password = $this->get_q('password', '');


            $url = IMOREDOMAIN . "memberApi.php?action=ajax_fnb_login&username=" . urlencode($email) . "&password=" . urlencode($this->aes->encrypt($password));

            $memberData = json_decode($this->Member->go_curl($url), 1);

            if ($memberData['ret']) {
                return showMsgCode($memberData['msg'], $this->selfurl . '?action=login_fnb');
            } else {

                $this->Member->login_succeed($memberData);

                $this->Points->points_record($memberData['memberInfo']['id'], 'login', "");

                $toURL = $this->s_param('toURL') ? $this->s_param('toURL') : "/";
                $this->userobj->set('toURL', '');
                $this->redirect($toURL);
            }
        } else {
            $toURL = $this->set_toURL();
            $this->tmpl_assign('selfurl', $this->selfurl);
            $this->tmpl_assign('NO_USERNAME', NO_USERNAME);
            $this->tmpl_assign('NO_PASSWORD', NO_PASSWORD);
            $this->tmpl_assign('ERR_CAPTCHA', ERR_CAPTCHA);
            $this->tmpl_assign('toURL', urlencode($toURL));
            return $this->load_tmpl('loginFNB.php');
        }
    }

    function action_login_facebook() {
        $toURL = $this->set_toURL();

        $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

        $login_obj = getLibClass('Login');
        list($status, $info, $memberData, $fbData) = $login_obj->login_facebook($redirect_uri);

        if ($status) {
            //echo 'facebook login success';
            if ($memberData) {
                $facebook_member = $this->Member->getMemberFacebook($memberData['id']);
                if ($facebook_member['result'])
                    $memberData['facebook'] = $facebook_member['result'][0];

                $this->Points->points_record($memberData['id'], 'login', "");

                $this->userobj->set('memberData', $memberData);
                $toURL = $this->s_param('toURL') ? $this->s_param('toURL') : "/";
                $this->userobj->set('toURL', '');
                $this->redirect($toURL);
            }else {

                $this->userobj->set('facebookMemberData', $fbData);
                return showMsgCode(FACEBOOKLOGINSUCCEED, $this->selfurl . '?action=regFacebook&toURL=' . urlencode($toURL));
            }
        } else {
            return showMsgCode(ERR_FACEBOOKLOGIN, $this->selfurl . '?action=regFacebook&toURL=' . urlencode($toURL));

            //echo $info;
        }
    }

    function action_logout_facebook() {
        require_once(LIB_PATH . '/Vendor/Facebook/sdk/facebook.php');
        require_once(LIB_PATH . '/Vendor/Facebook/FaceBookApi.class.php');

        $fb_config = array();
        $fb_config['appId'] = FB_APPID;
        $fb_config['secret'] = FB_SECRET;
        $fb_config['scope'] = FB_SCOPE;
        $fb_config['redirect_uri'] = FB_REDIRECT_URI;
        $fb_config['cookie'] = FB_COOKIE;
        $fb_config['fileUpload'] = FB_FILEUPLOAD_SUPPORT;

        $facebook = new FaceBookApi($fb_config);
        echo $facebook->getLogoutUrl(1);
    }

    function action_regFacebook() {
        $this->userobj->set('memberData', '');
        if ($this->get_q('issubmit')) {

            $reg_p = $this->get_q('reg_p');
            $this->check_reg_p($reg_p, 'facebook');

            $postData = $this->Member->postData($reg_p);

            $url = IMOREDOMAIN . "memberApi.php?action=fb_register";

            $memberData = json_decode($this->Member->go_curl($url, $postData), 1);
            if ($memberData['ret'] == 1) {

                $in_data = array();
                $in_data['id'] = $memberData['id'];
                $in_data['photo'] = $memberData['photo'];
                $in_data['username'] = $memberData['memberData']['username'];
                $in_data['email'] = $memberData['memberData']['email'];
                $in_data['phone'] = $memberData['memberData']['phone'];
                $in_data['idcard'] = $memberData['memberData']['idcard'];
                $in_data['birthday'] = $memberData['memberData']['birthday'];
                $in_data['salutation'] = $memberData['memberData']['salutation'];

                $in_data['regip'] = getip();
                $in_data['addtime'] = date('Y-m-d H:i:s', time());
                $in_data['logintime'] = date('Y-m-d H:i:s', time());
                $in_data['email_status'] = 1;
                $in_data['acttime'] = date('Y-m-d H:i:s', time());
                $in_data['email_receive'] = $memberData['memberData']['email_receive'];
                db_add("member_imore", $in_data);
                $this->Points->points_record($memberData['id'], 'reg');


                $this->userobj->set('memberData', $in_data);

                return showMsgCode(REGFACEBOOKSUCCEED, $this->selfurl . '?action=regTrial');
            } else {
                return showMsgCode($memberData['msg'], $this->selfurl . '?action=regFacebook');
            }
        } else {

            $facebookMemberData = $this->s_param('facebookMemberData');
            if (!$facebookMemberData) {
                return showMsgCode(HAS_FACEBOOKLOGIN, $this->selfurl . '?action=login');
                $toURL = $this->selfurl . '?action=login_facebook';
                $this->userobj->set('toURL', $toURL);
            }

            $birthday_date = date("Y-m-d", strtotime($facebookMemberData['birthday_date']));

            $this->tmpl_assign('birthday_date', $birthday_date);

            $this->tmpl_assign('facebookMemberData', $facebookMemberData);

            $this->tmpl_assign('ERR_FNBUSERNAME', ERR_FNBUSERNAME);
            $this->tmpl_assign('ERR_USERNAME', ERR_USERNAME);
            $this->tmpl_assign('HAS_USERNAME', HAS_USERNAME);
            $this->tmpl_assign('HAS_EMAIL', HAS_EMAIL);
            $this->tmpl_assign('ERR_PWD', ERR_PWD);
            $this->tmpl_assign('ERR_EMAIL', ERR_EMAIL);
            $this->tmpl_assign('ERR_SALUTATION', ERR_SALUTATION);
            $this->tmpl_assign('ERR_PHONE', ERR_PHONE);
            $this->tmpl_assign('ERR_BIRTH', ERR_BIRTH);
            $this->tmpl_assign('ERR_CAPTCHA', ERR_CAPTCHA);
            $this->tmpl_assign('ERR_CPWD', ERR_CPWD);
            $this->tmpl_assign('selfurl', $this->selfurl);
            $this->tmpl_assign('CHECKING', CHECKING);
            $this->tmpl_assign('HAS_INVITE_PIC', HAS_INVITE_PIC);
            $this->tmpl_assign('NO_AGREE', NO_AGREE);
            $this->tmpl_assign('toURL', urlencode($toURL));
            $global_url = $this->param('URL');
            $this->tmpl_assign('global_url', $global_url);

            return $this->load_tmpl('memberRegFacebook.php');
        }
    }

    function action_reg() {
        $this->userobj->set('memberData', '');
        if ($this->get_q('issubmit')) {
            // $captcha = $this->get_q('captcha');

            $toURL = $this->get_toURL();

            $reg_p = $this->get_q('reg_p');
            $friend = $this->get_q('friend');


            $this->check_reg_p($reg_p);

            $x = $this->get_q('x');
            $y = $this->get_q('y');
            $w = $this->get_q('w');
            $h = $this->get_q('h');

            $avatar_pic = $reg_p['avatar'];
            if ($x) {
                $avatar_pic = cropPhoto($x, $y, $w, $h, 84, 84, $avatar_pic);
                $avatar_pic = FB_REDIRECT_URI . str_replace($this->param('WWW'), '', $avatar_pic);
            } else {
                $avatar_pic = $this->param('URL') . substr(PhotoResize($avatar_pic, 84, 84), 1);
            }
            $reg_p['avatar'] = $avatar_pic;
            $reg_p['salutation'] = $reg_p['sex'];

            $postData = $this->Member->postData($reg_p);

            $url = IMOREDOMAIN . "memberApi.php?action=register";

            $memberData = json_decode($this->Member->go_curl($url, $postData), 1);

            if ($memberData['ret'] == 1) {

                $in_data = array();
                $in_data['id'] = $memberData['id'];
                $in_data['photo'] = $memberData['photo'];
                $in_data['username'] = $memberData['memberData']['username'];
                $in_data['email'] = $memberData['memberData']['email'];
                $in_data['ename'] = $reg_p['ename'];
                $in_data['phone'] = $memberData['memberData']['phone'];
                $in_data['idcard'] = $memberData['memberData']['idcard'];
                $in_data['birthday'] = $memberData['memberData']['birthday'];
                $in_data['sex'] = $reg_p['sex'];

                $in_data['regip'] = getip();
                $in_data['addtime'] = date('Y-m-d H:i:s', time());
                $in_data['logintime'] = date('Y-m-d H:i:s', time());
                $in_data['email_status'] = 0;
                $in_data['lastmodify'] = date('Y-m-d H:i:s', time());
                $in_data['email_receive'] = $memberData['memberData']['email_receive'];

                db_add("member_imore", $in_data);

                $this->userobj->set('memberData', $in_data);

                $member = $memberData['memberData'];
                $member['id'] = $memberData['id'];

                $this->Points->points_record($member['id'], 'reg');

                if (!empty($friend) && $friend <> $member['username']) {
                    $checknameresult = $this->Member->getMember(0, '', $friend);

                    if ($checknameresult['result']) {
                        //sleep(1);
                        $this->Points->points_record($checknameresult['result'][0]['id'], 'refer_friend', str_replace("[朋友名稱]", $member['username'], REFER_FRIEND_LANG));
                    }
                }


                $global_url = $this->param('URL');

                $url = $global_url . $this->selfurl . "?action=activation&membercode=" . md5($this->activationKey . $member['id'] . $member['username'] . $member['email']) . "&mid=" . $member['id'];

                $this->email_obj->send($member['id'], "become_member", array('username' => $member['username'], 'emailUrl' => $url));

                $this->userobj->set('newMember', array('newMember' => 1));
                $sql = "update member_imore set expert_approval=2 where id=" . mysql_escape_string($memberData['id']);
                db_handle($sql);




                return showMsgCode(REGSUCCEED, $this->selfurl . '?action=regTrial&toURL=' . urlencode($toURL));
            } else {
                return showMsgCode($memberData['msg'], $this->selfurl . '?action=reg');
            }
        } else {

            $toURL = $this->set_toURL();
            $friend = $this->get_q("friend");
            $this->tmpl_assign('ERR_FNBUSERNAME', ERR_FNBUSERNAME);
            $this->tmpl_assign('ERR_USERNAME', ERR_USERNAME);
            $this->tmpl_assign('HAS_USERNAME', HAS_USERNAME);
            $this->tmpl_assign('HAS_EMAIL', HAS_EMAIL);
            $this->tmpl_assign('ERR_PWD', ERR_PWD);
            $this->tmpl_assign('ERR_EMAIL', ERR_EMAIL);
            $this->tmpl_assign('ERR_SALUTATION', ERR_SALUTATION);
            $this->tmpl_assign('ERR_PHONE', ERR_PHONE);
            $this->tmpl_assign('ERR_BIRTH', ERR_BIRTH);
            $this->tmpl_assign('ERR_CAPTCHA', ERR_CAPTCHA);
            $this->tmpl_assign('ERR_CPWD', ERR_CPWD);
            $this->tmpl_assign('selfurl', $this->selfurl);
            $this->tmpl_assign('CHECKING', CHECKING);
            $this->tmpl_assign('HAS_INVITE_PIC', HAS_INVITE_PIC);
            $this->tmpl_assign('NO_AGREE', NO_AGREE);
            $this->tmpl_assign('toURL', urlencode($toURL));
            $this->tmpl_assign('HAS_PHONE', HAS_PHONE);
            $this->tmpl_assign('friend', $friend);

            $global_url = $this->param('URL');
            $this->tmpl_assign('global_url', $global_url);



            return $this->load_tmpl('memberReg.php');
        }
    }

    function action_regTrial() {
        $toURL = $this->selfurl . '?action=regTrial';
        $memberData = $memberData = $this->is_login($toURL);
        $toURL = $this->set_toURL();

        if ($this->get_q('issubmit')) {
            $toURL = $this->get_toURL();

            $profession = $this->get_q('profession');
            if (!$profession)
                return showMsgCode(HAS_PROFESSION, $this->selfurl . '?action=regTrial&toURL=' . urlencode($toURL));
            $body = $this->get_q('body');
            if (!$body)
                return showMsgCode(HAS_BODY, $this->selfurl . '?action=regTrial&toURL=' . urlencode($toURL));
            $face_skin = $this->get_q('face_skin');
            if (!$face_skin)
                return showMsgCode(HAS_FACE_SKIN, $this->selfurl . '?action=regTrial&toURL=' . urlencode($toURL));
            $body_skin = $this->get_q('body_skin');
            if (!$body_skin)
                return showMsgCode(HAS_BODY_SKIN, $this->selfurl . '?action=regTrial&toURL=' . urlencode($toURL));
            $hair = $this->get_q('hair');
            if (!$hair)
                return showMsgCode(HAS_HAIR, $this->selfurl . '?action=regTrial&toURL=' . urlencode($toURL));

            $address = $this->get_q('address');
            if (!$address)
                return showMsgCode(HAS_ADDRESS, $this->selfurl . '?action=regTrial&toURL=' . urlencode($toURL));

            $insert_data = array();
            $insert_data['mid'] = $memberData['id'];
            $insert_data['id'] = $memberData['id'];
            $insert_data['profession'] = $profession;
            $insert_data['body'] = $body;
            $insert_data['face_skin'] = $face_skin;
            $insert_data['body_skin'] = $body_skin;
            $insert_data['hair'] = $hair;
            $insert_data['address'] = $address;
            $insert_data['updatetime'] = date('Y-m-d H:i:s');
            $insert_data['code'] = md5($memberData['id'] . $this->activationKey);

            $itrial_member = $this->Member->getMemberCondition($memberData['id']);
            if ($itrial_member['result']) {

                $session['id'] = $itrial_member['result'][0]['id'];
                $session['email_status'] = $itrial_member['result'][0]['email_status'];
                $session['email'] = $itrial_member['result'][0]['email'];
                $session['photo'] = $itrial_member['result'][0]['photo'];
                $session['username'] = $itrial_member['result'][0]['username'];
                $session['member_type'] = $itrial_member['result'][0]['member_type'];
                $this->userobj->set('memberData', $session);


                $insert_data['interest'] = $itrial_member['result'][0]['interest'];
                $insert_data['homepage'] = $itrial_member['result'][0]['homepage'];
                $insert_data['age_group'] = $itrial_member['result'][0]['age_group'];
                $insert_data['question'] = $itrial_member['result'][0]['question'];
                $insert_data['marital_status'] = $itrial_member['result'][0]['marital_status'];
                $insert_data['brand'] = $itrial_member['result'][0]['brand'];
                $insert_data['product_layout_category'] = $itrial_member['result'][0]['product_layout_category'];
                $insert_data['check_status'] = $itrial_member['result'][0]['check_status'];
                $insert_data['status'] = $itrial_member['result'][0]['status'];
                $insert_data['addtime'] = $itrial_member['result'][0]['addtime'];
                $insert_data['blogPage'] = $itrial_member['result'][0]['blogPage'];
                $insert_data['reportURL'] = $itrial_member['result'][0]['reportURL'];
                $insert_data['photo'] = $itrial_member['result'][0]['photo'];
            } else {
                $this->Points->points_record($memberData['id'], 'reg_trial', "");
            }

            $insertkeyvalue = $insertvalue = '';
            if ($insert_data) {
                foreach ($insert_data as $key => $result) {
                    $insertkeyvalue.= $insertkeyvalue ? " ," . $key : $key;
                    $insertvalue.= $insertvalue ? " ,'" . mysql_escape_string($result) . "'" : "'" . mysql_escape_string($result) . "'";
                }
            }

            $sql = "REPLACE INTO member_condition ($insertkeyvalue)
                	VALUES($insertvalue)";

            $param = array();
            db_handle($sql, $param);

            $post_data = array();
            $post_data['lastmodify'] = date('Y-m-d H:i:s');
            $condition = "id=" . mysql_escape_string($memberData['id']);
            db_update('member_imore', $post_data, $condition);

            $sql = "update member_imore set expert_approval=3 where expert_approval<3 and id=" . mysql_escape_string($memberData['id']);
            db_handle($sql);
            $sql = "update member_imore set  member_type =1 where member_type<1 and id=" . mysql_escape_string($memberData['id']);
            db_handle($sql);

            $this->Member->set_newSession($memberData['id']);

            $this->redirect($this->selfurl . "?action=regExpert&toURL=" . urlencode($toURL));
        } else {

            $member = $this->Member->getMemberCondition($memberData['id']);
            $data = array();
            if ($member['result'])
                $data = $member['result'][0];
            if ($data['address'] && $data['homepage']) {
                $this->redirect($this->selfurl . "?action=MyProfile&toURL=" . urlencode($toURL));
            }

            $toURL = $this->set_toURL();

            $layout['profession'] = $this->Lookup->getLookup('profession');
            $layout['profession'] = $layout['profession']['result'];
            $layout['body'] = $this->Lookup->getLookup('body');
            $layout['body'] = $layout['body']['result'];
            $layout['face_skin'] = $this->Lookup->getLookup('face_skin');
            $layout['face_skin'] = $layout['face_skin']['result'];
            $layout['body_skin'] = $this->Lookup->getLookup('body_skin');
            $layout['body_skin'] = $layout['body_skin']['result'];
            $layout['hair'] = $this->Lookup->getLookup('hair');
            $layout['hair'] = $layout['hair']['result'];
            $layout['selfurl'] = $this->selfurl;
            $layout['member'] = $data;

            $this->tmpl_assign('layout', $layout);
            $this->tmpl_assign('ERR_PHONE', ERR_PHONE);
            $this->tmpl_assign('HAS_PROFESSION', HAS_PROFESSION);
            $this->tmpl_assign('HAS_BODY', HAS_BODY);
            $this->tmpl_assign('HAS_FACE_SKIN', HAS_FACE_SKIN);
            $this->tmpl_assign('HAS_BODY_SKIN', HAS_BODY_SKIN);
            $this->tmpl_assign('HAS_HAIR', HAS_HAIR);
            $this->tmpl_assign('HAS_ADDRESS', HAS_ADDRESS);
            $this->tmpl_assign('ERR_BIRTH', ERR_BIRTH);
            $this->tmpl_assign('CHECKING', CHECKING);
            $this->tmpl_assign('HAS_PHONE', HAS_PHONE);

            $this->tmpl_assign('toURL', urlencode($toURL));
            return $this->load_tmpl('memberRegTrial.php');
        }
    }

    function action_regExpert() {
        $toURL = $this->selfurl . '?action=regExpert';
        $memberData = $this->is_login($toURL);
        $toURL = $this->set_toURL();

        if ($this->get_q('issubmit')) {

            $is_add_points = 0;
            $member = $this->Member->getMemberCondition($memberData['id']);
            $data = array();
            if ($member['result'])
                $data = $member['result'][0];
            if (!$data['homepage']) {
                $is_add_points = 1;
            }

            $toURL = $this->get_toURL();

            $brand = $this->get_q('brand_val');

            $question_q = $this->get_q('question');
            if (!$question_q)
                return showMsgCode(HAS_QUESTION, $this->selfurl . '?action=regExpert&toURL=' . urlencode($toURL));
            $question = implode(",", $question_q);

            $interest_q = $this->get_q('interest');
            if (!$interest_q)
                return showMsgCode(HAS_INTEREST, $this->selfurl . '?action=regExpert&toURL=' . urlencode($toURL));
            $interest = implode(",", $interest_q);

            $homepage = $this->get_q('homepage');
            if (!$homepage)
                return showMsgCode(HAS_HOMEPAGE, $this->selfurl . '?action=regExpert&toURL=' . urlencode($toURL));

            $photo = $this->get_q('photo');
            if (!$photo)
                return showMsgCode(HAS_INVITE_PHOTO, $this->selfurl . '?action=regExpert&toURL=' . urlencode($toURL));

            $blogPage = $this->get_q('blogPage');
            $reportURL = $this->get_q('reportURL');

            $post_data = array();
            $post_data['brand'] = $brand;
            $post_data['question'] = $question;
            $post_data['interest'] = $interest;
            $post_data['homepage'] = $homepage;
            $post_data['blogPage'] = $blogPage;
            $post_data['reportURL'] = $reportURL;
            $post_data['photo'] = $photo;

            $post_data['updatetime'] = date('Y-m-d H:i:s');
            $condition = "mid=" . mysql_escape_string($memberData['id']);
            db_update('member_condition', $post_data, $condition);

            $post_data_q = array();
            $post_data_q['lastmodify'] = date('Y-m-d H:i:s');
            $condition = "id=" . mysql_escape_string($memberData['id']);
            db_update('member_imore', $post_data_q, $condition);

            //return showMsgCode("更新成功",$this->selfurl.'?action=regSucceed&toURL='.urlencode($toURL));
            $sql = "update member_imore set expert_approval=4 where (expert_approval<=4 or expert_approval=6 ) and id=" . mysql_escape_string($memberData['id']);
            db_handle($sql);

            $this->Member->set_newSession($memberData['id']);

            if ($is_add_points)
                $this->Points->points_record($memberData['id'], 'reg_expert', "");

            $this->redirect($this->selfurl . "?action=regSucceed&toURL=" . urlencode($toURL));
        } else {
            $member = $this->Member->getMemberCondition($memberData['id']);
            $data = array();
            if ($member['result'])
                $data = $member['result'][0];
            if (!$data) {
                return showMsgCode(NOTGENERAL, $this->selfurl . '?action=regTrial&toUrl=' . urlencode($toURL));
            } else if ($data['address'] && $data['homepage']) {
                $this->redirect($this->selfurl . "?action=MyProfile&toURL=" . urlencode($toURL));
            }


            $toURL = $this->set_toURL();
            $layout['brand'] = $this->Lookup->getLookup('brand');
            $layout['brand'] = $layout['brand']['result'];
            $layout['question'] = $this->Lookup->getLookup('question');
            $layout['question'] = $layout['question']['result'];
            $layout['interest'] = $this->Lookup->getLookup('interest');
            $layout['interest'] = $layout['interest']['result'];
            $layout['selfurl'] = $this->selfurl . '?action=regExpert';
            $brand_key = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

            $brandarr = array();
            if ($layout['brand']) {
                foreach ($layout['brand'] as $value) {
                    $brandarr[$value['id']] = $value['name'];
                }
            }
            $layout['brand'] = $this->assort_brand($brand_key, $brandarr);

            $layout['member'] = array();
            $itrial_member = $this->Member->getMemberCondition($memberData['id']);
            if ($itrial_member['result']) {
                $layout['member'] = $itrial_member['result'][0];
                $layout['member']['interest'] = explode(',', $layout['member']['interest']);
                $layout['member']['question'] = explode(',', $layout['member']['question']);
                $layout['member']['brand'] = explode(',', $layout['member']['brand']);
            }


            $this->tmpl_assign("brand_key", $brand_key);
            $this->tmpl_assign('layout', $layout);
            $this->tmpl_assign('HAS_HOMEPAGE', HAS_HOMEPAGE);
            $this->tmpl_assign('HAS_QUESTION', HAS_QUESTION);
            $this->tmpl_assign('HAS_INTEREST', HAS_INTEREST);
            $this->tmpl_assign('HAS_BRAND', HAS_BRAND);

            $this->tmpl_assign('toURL', urlencode($toURL));
            $this->tmpl_assign('HAS_INVITE_PIC', HAS_INVITE_PHOTO);
            $global_url = $this->param('URL');
            $this->tmpl_assign('global_url', $global_url);

            return $this->load_tmpl('memberRegExpert.php');
        }
    }

    function action_regSucceed() {
        $toURL = $this->selfurl . '?action=regSucceed';
        $memberData = $this->is_login($toURL);
        $toURL = $this->set_toURL();

        $memberCondition = $this->Member->getMemberCondition($memberData['id']);

        $data[1] = $this->Product->getProductList(1, 1, "TB1.apply_starttime desc", 1, 6);
        $data[2] = $this->Product->getProductList(5, 1, "TB1.apply_starttime desc", 1, 6);
        foreach ($data as $k => $v) {
            foreach ($v['result'] as $k1 => $v1) {
                $data[$k]['result'][$k1]['_difTime'] = explode(" ", str_diff_time($v1['apply_endtime']));
                $data[$k]['result'][$k1]['_photo_url'] = PhotoResize($v1['cover'], 158, 158);
            }
        }
        $this->tmpl_assign('data', $data);
        $this->tmpl_assign('toURL', $toURL);
        $newMember = $this->s_param('newMember');
        $this->tmpl_assign('newMember', $newMember);
        $this->userobj->set('newMember', "");

        if ($memberCondition['result'])
            $this->tmpl_assign('memberCondition', $memberCondition['result'][0]);

        return $this->load_tmpl('memberRegSucceed.php');
    }

    function check_reg_p($reg_p, $type = 'member') {


        if ($type == 'member') {
            $url = $this->selfurl . '?action=reg';
        } elseif ($type == 'facebook') {
            $url = $this->selfurl . '?action=regFacebook';
        }

        //用戶名
        $reg_p['username'] = trim($reg_p['username']);
        if (preg_match("/^fnb_/i", $reg_p['username']))
            return showMsgCode(ERR_FNBUSERNAME, $url);
        if (!preg_match("/^[a-z][a-z0-9_]{2,14}$/i", $reg_p['username']))
            return showMsgCode(ERR_USERNAME, $url);

        $checknameresult = $this->Member->getMember(0, '', $reg_p['username']);
        //$this->getUser($reg_p['username']);
        if ($checknameresult['result']) {
            return showMsgCode(HAS_USERNAME, $url);
        }
        //email
        $reg_p['email'] = trim($reg_p['email']);
        if (!is_email($reg_p['email']))
            return showMsgCode(ERR_EMAIL, $url);
        $checkemailresult = $this->Member->getMember(0, $reg_p['email']);
        //$this->getUser('',$reg_p['email']);
        if ($checkemailresult['result']) {
            return showMsgCode(HAS_EMAIL, $url);
        }

        //密碼
        if (!preg_match("/^.{6,16}$/i", $reg_p['password'])) {
            return showMsgCode(ERR_PWD, $url);
        }

        //稱謂
        $reg_p['sex'] = intval($reg_p['sex']);
        if ($reg_p['sex'] != 1 && $reg_p['sex'] != 2 && $reg_p['sex'] != 3) {
            return showMsgCode("請選擇性別", $url);
        }
        //手提電話
        if (!preg_match("/^[0-9]{1,}$/", $reg_p['phone'])) {
            return showMsgCode(ERR_PHONE, $url);
        }
        $checkphoneresult = $this->Member->getMember(0, "", "", 0, "", 1, 1, $reg_p['phone']);
        if ($checkphoneresult['result']) {
            return showMsgCode(HAS_PHONE, $url);
        }

        //身份證
        if (!preg_match("/^[a-z][0-9a-z][0-9]{2}$/i", $reg_p['idcard'])) {
            return showMsgCode(ERR_IDCARD, $url);
        }

        //出生日期
        if (!check_birthday($reg_p['birthday'])) {
            return showMsgCode(ERR_BIRTH, $url);
        }
    }

    //牌子按$brand_key歸類
    function assort_brand($brand_key, $brand_array) {
        $return = array();
        foreach ($brand_array as $key => $value) {
            if (!preg_match("/^[a-z]/i", $value)) {
                $return['其他'][$key] = $value;
                unset($brand_array[$key]);
            }
        }
        foreach ($brand_key as $value1) {
            foreach ($brand_array as $key2 => $value2) {
                if (preg_match("/^" . $value1 . "/i", $value2)) {
                    $return[$value1][$key2] = $value2;
                }
            }
        }
        return $return;
    }

    function action_ajax_checkName() {
        $username = $this->get_q('username');
        if (!preg_match("/^[a-z][a-z0-9_]{2,14}$/i", $username)) {
            return -1;
        }
        if (preg_match("/^fnb_/i", $username)) {
            return -2;
        }
        $checknameresult = $this->Member->getMember(0, '', $username);
        //$this->getUser($username);
        if ($checknameresult['result']) {
            return -3;
        } else {
            return 1;
        }
    }

    function action_ajax_checkEmail() {
        $email = $this->get_q('email');

        if (!is_email($email))
            return -4;
        $checkemailresult = $this->Member->getMember(0, $email);
        //$this->getUser('',$email);
        if ($checkemailresult['result']) {
            return -6;
        } else {
            return 1;
        }
    }

    function action_ajax_uploadAvatar() {

        $root = $this->param('WWW');
        $upload_status = uploadfiles($root . '/upload/avatar');
        $global_url = $this->param('URL');

        foreach ($upload_status as $k => $v) {
            if ($v['code'] === 1) {
                $upload_status[$k]['src'] = $root . substr(showphoto($upload_status[$k]['path'], 400, 400), 1);
                //$upload_status[$k]['thumb'] = $global_url . PhotoResize($upload_status[$k]['path'], 84, 84);
                $upload_status[$k]['thumb'] = $global_url . substr(showphoto($upload_status[$k]['path'], 400, 400), 1);
            }
        }

        echo '<script>' . str_replace('$res', json_encode($upload_status), $_POST['callback_script']) . '</script>';
    }

    function action_ajax_checkCaptcha() {
        $captcha = $this->get_q('captcha');

        $cap_obj = getLibClass('Captcha');
        if ($cap_obj->check($captcha, 0)) {
            echo 'true';
        } else {
            echo 'false';
        }
    }

    function action_ajax_memberBar() {

        $memberData = $this->s_param('memberData');

        //$url = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
        $url = $this->get_q('url');

        $this->tmpl_assign('selfurl', $this->selfurl);
        $this->tmpl_assign('url', urlencode($url));
        $this->tmpl_assign('memberData', $memberData);

        return $this->load_tmpl('memberBar.php');
    }

    function action_activation() {
        $membercode = $this->get_q('membercode');
        if (!$membercode)
            return showMsgCode(ERR_MEMBERCODE, "/");

        $mid = intval($this->get_q('mid'));
        if (!$mid)
            return showMsgCode(ERR_MEMBERID, "/");

        $member = array();
        $user = $this->Member->getMember($mid);

        //$this->getUser('','',$mid);
        if ($user['result'])
            $member = $user['result'][0];
        if ($member) {
            if (md5($this->activationKey . $member['id'] . $member['username'] . $member['email']) == $membercode) {
                if ($member['email_status'] != 1) {

                    $url = IMOREDOMAIN . "activationAPI.php";
                    $postData = "membercode=" . $membercode . "&mid=" . $mid;

                    $memberData = json_decode($this->Member->go_curl($url, $postData), 1);
                    if ($memberData['ret'] == 1) {
                        $post_data = array();
                        $post_data['email_status'] = 1;
                        $post_data['acttime'] = date('Y-m-d H:i:s');
                        $condition = "id=" . mysql_escape_string($member['id']);
                        db_update('member_imore', $post_data, $condition);
                        return showMsgCode(ACTIVATIONSUCCEED, "/");
                    }
                } else {

                    return showMsgCode(HAS_ENABLED, "/");
                }
            } else {
                return showMsgCode(ERR_MEMBERCODE, "/");
            }
        } else {
            return showMsgCode(HAS_MEMBERID, "/");
        }
    }

    function action_resendActivation() {

        $memberData = $this->s_param('memberData');

        $user = $this->Member->getMember($memberData['id']);
        //$this->getUser('','',$memberData['id']);

        $member = array();
        if ($user['result'])
            $member = $user['result'][0];
        if ($member['email_status'] != 1) {
            $global_url = $this->param('URL');
            $url = $global_url . $this->selfurl . "?action=activation&membercode=" . md5($this->activationKey . $member['id'] . $member['username'] . $member['email']) . "&mid=" . $member['id'];


            $this->email_obj->send($member['id'], "become_member", array('username' => $member['username'], 'emailUrl' => $url));
        } else {
            return showMsgCode(HAS_ENABLED, "/");
        }


        return showMsgCode(ACTIVATIONEMAILSUCCEED, "/");
    }

    function action_forgetPassword() {
        $memberData = $this->userobj->set('memberData', '');

        $email = $this->get_q('email');
        if ($email) {
            $user = $this->Member->getMember(0, $email);
            //$this->getUser('',$email);
            $member = array();
            if ($user['result']) {
                $member = $user['result'][0];
                $global_url = $this->param('URL');

                $url = $global_url . $this->selfurl . "?action=changePassword&membercode=" . md5($this->activationKey . $member['id'] . $member['username'] . $member['email'] . date('Ymd')) . "&mid=" . $member['id'];

                $this->email_obj->send($member['id'], "forgetPassword", array('username' => $member['username'], 'emailUrl' => $url));
                return showMsgCode(SENDED, "/");
            } else {
                return showMsgCode(ERR_FORGET_EMAIL, $this->selfurl . '?action=forgetPassword');
            }
        } else {

            $this->tmpl_assign('selfurl', $this->selfurl);
            $this->tmpl_assign('memberData', $memberData);
            $this->tmpl_assign('ERR_EMAIL', ERR_EMAIL);
            return $this->load_tmpl('forgetPassword.php');
        }
    }

    function action_changePassword() {
        $membercode = $this->get_q('membercode');
        if (!$membercode)
            return showMsgCode(ERR_MEMBERCODE, "/");

        $mid = intval($this->get_q('mid'));
        if (!$mid)
            return showMsgCode(ERR_MEMBERID, "/");

        $member = array();
        $user = $this->Member->getMember($mid);
        //$this->getUser('','',$mid);
        if ($user['result'])
            $member = $user['result'][0];
        if ($member) {
            if (md5($this->activationKey . $member['id'] . $member['username'] . $member['email'] . date('Ymd')) == $membercode) {
                $password = $this->get_q('password');
                if ($password) {
                    $password = $this->aes->encrypt($password);

                    $url = IMOREDOMAIN . "changePasswordAPI.php";
                    $postData = "membercode=" . $membercode . "&mid=" . $mid . "&password=" . urlencode($password);
                    $memberData = json_decode($this->Member->go_curl($url, $postData), 1);
                    if ($memberData['ret'] == 1) {
                        $this->userobj->set('memberData', '');
                        return showMsgCode(CHANGED, "/");
                    } else {
                        return showMsgCode($memberData['msg'], "/");
                    }
                } else {

                    $this->tmpl_assign('username', $member['username']);
                    $this->tmpl_assign('membercode', $membercode);
                    $this->tmpl_assign('mid', $mid);
                    $this->tmpl_assign('ERR_PWD', ERR_PWD);
                    $this->tmpl_assign('ERR_CPWD', ERR_CPWD);
                    return $this->load_tmpl('changePassword.php');
                }
            } else {
                return showMsgCode(ERR_MEMBERCODE, "/");
            }
        } else {
            return showMsgCode(HAS_MEMBERID, "/");
        }
    }

    function action_ajax_checkPhone() {
        $phone = $this->get_q('phone');

        if (!preg_match("/^[0-9]{8,11}$/", $phone)) {
            return -1;
        }

        $checkphoneresult = $this->Member->getMember(0, "", "", 0, "", 1, 1, $phone);
        if ($checkphoneresult['result']) {
            return -2;
        } else {
            return 1;
        }
    }

    function action_MyProfile() {
        $toURL = $this->selfurl . '?action=MyProfile';
        $memberData = $this->is_login($toURL);
        $toURL = $this->set_toURL();

        if ($this->get_q('issubmit')) {
            //$captcha = $this->get_q('captcha');
            $toURL = $this->get_toURL();

            $reg_p = $this->get_q('reg_p');

            $reg_p['id'] = $memberData['id'];
            $password = $this->get_q('password');
            $opassword = $this->get_q('opassword');
            $email = $this->get_q('email');
            $photo = $this->get_q('photo');
            $profession = $this->get_q('profession');
            if (!$profession)
                return showMsgCode(HAS_PROFESSION, $this->selfurl . '?action=MyProfile&toURL=' . urlencode($toURL));
            $body = $this->get_q('body');
            if (!$body)
                return showMsgCode(HAS_BODY, $this->selfurl . '?action=MyProfile&toURL=' . urlencode($toURL));
            $face_skin = $this->get_q('face_skin');
            if (!$face_skin)
                return showMsgCode(HAS_FACE_SKIN, $this->selfurl . '?action=MyProfile&toURL=' . urlencode($toURL));
            $body_skin = $this->get_q('body_skin');
            if (!$body_skin)
                return showMsgCode(HAS_BODY_SKIN, $this->selfurl . '?action=MyProfile&toURL=' . urlencode($toURL));
            $hair = $this->get_q('hair');
            if (!$hair)
                return showMsgCode(HAS_HAIR, $this->selfurl . '?action=MyProfile&toURL=' . urlencode($toURL));

            $address = $this->get_q('address');
            if (!$address)
                return showMsgCode(HAS_ADDRESS, $this->selfurl . '?action=MyProfile&toURL=' . urlencode($toURL));
            //$brand = $this->get_q('brand');
            $brand_q = $this->get_q('brand_val');
            if (!$brand_q)
                return showMsgCode(HAS_BRAND, $this->selfurl . '?action=MyProfile&toURL=' . urlencode($toURL));

            $array = explode(",", $brand_q);
            if ($array) {
                foreach ($array as $key => $value) {
                    if (!$value) {
                        unset($array[$key]);
                    }
                }
            }
            $brand = implode(",", $array);

            //$brand = implode(",", $brand);

            $question_q = $this->get_q('question');
            if (!$question_q)
                return showMsgCode(HAS_QUESTION, $this->selfurl . '?action=MyProfile&toURL=' . urlencode($toURL));
            $question = implode(",", $question_q);

            $interest_q = $this->get_q('interest');
            if (!$interest_q)
                return showMsgCode(HAS_INTEREST, $this->selfurl . '?action=MyProfile&toURL=' . urlencode($toURL));
            $interest = implode(",", $interest_q);

            $homepage = $this->get_q('homepage');
            if (!$homepage)
                return showMsgCode(HAS_HOMEPAGE, $this->selfurl . '?action=MyProfile&toURL=' . urlencode($toURL));

            $photo2 = $this->get_q('photo2');
            if (!$photo2)
                return showMsgCode(HAS_INVITE_PHOTO, $this->selfurl . '?action=MyProfile&toURL=' . urlencode($toURL));

            $blogPage = $this->get_q('blogPage');
            $reportURL = $this->get_q('reportURL');

            if ($reg_p['email_receive'] <> 1)
                $reg_p['email_receive'] = 0;

            if ($photo) {
                $x = $this->get_q('x');
                $y = $this->get_q('y');
                $w = $this->get_q('w');
                $h = $this->get_q('h');

                $avatar_pic = $photo;
                if ($x) {
                    $avatar_pic = cropPhoto($x, $y, $w, $h, 84, 84, $avatar_pic);
                    $avatar_pic = FB_REDIRECT_URI . str_replace($this->param('WWW'), '', $avatar_pic);
                } else {
                    $avatar_pic = $this->param('URL') . substr(PhotoResize($avatar_pic, 84, 84), 1);
                }
                $reg_p['photo'] = $avatar_pic;
            }

            if ($password || ($email && $email <> $memberData['email'])) {
                if (!$opassword)
                    return showMsgCode(HAS_OPASSWORD, $this->selfurl . '?action=MyProfile');
            }

            if ($password)
                $reg_p['password'] = $password;
            if ($opassword) {
                $reg_p['opassword'] = $opassword;
                $reg_p['username'] = $memberData['username'];
            }
            if ($email && $email <> $memberData['email'])
                $reg_p['email'] = $email;

            $reg_p['salutation'] = $reg_p['sex'];
            $postData = $this->Member->postData($reg_p);

            $url = IMOREDOMAIN . "memberApi.php?action=myRegister";
            $memberJsonData = json_decode($this->Member->go_curl($url, $postData), 1);

            if ($memberJsonData['ret'] == 1) {

                $post_data = $reg_p;
                unset($post_data["opassword"]);
                unset($post_data["password"]);

                if ($email && $email <> $memberData['email']) {
                    $post_data['email'] = $email;
                    $post_data['email_status'] = 0;
                    $this->resend_email($email);
                }
                if ($photo)
                    $post_data['photo'] = $memberJsonData['memberData']['photo'];

                $condition = "id=" . mysql_escape_string($memberData['id']);
                $post_data['lastmodify'] = date('Y-m-d H:i:s');
                db_update('member_imore', $post_data, $condition);


                $update_data = array();
                $update_data['mid'] = $memberData['id'];
                $update_data['id'] = $memberData['id'];
                $update_data['profession'] = $profession;
                $update_data['body'] = $body;
                $update_data['face_skin'] = $face_skin;
                $update_data['body_skin'] = $body_skin;
                $update_data['hair'] = $hair;
                $update_data['address'] = $address;
                $update_data['updatetime'] = date('Y-m-d H:i:s');
                $update_data['code'] = md5($memberData['id'] . $this->activationKey);
                $update_data['brand'] = $brand;
                $update_data['question'] = $question;
                $update_data['interest'] = $interest;
                $update_data['homepage'] = $homepage;
                $update_data['blogPage'] = $blogPage;
                $update_data['reportURL'] = $reportURL;
                $update_data['photo'] = $photo2;

                $condition = "mid=" . mysql_escape_string($memberData['id']);
                db_update('member_condition', $update_data, $condition);


                $this->Member->set_newSession($memberData['id']);

                $this->redirect($toURL);
                //$this->redirect($this->selfurl."?action=regTrial&toURL=".urlencode($toURL));
            }else {
                return showMsgCode($memberJsonData['msg'], $this->selfurl . '?action=MyProfile&toURL=' . urlencode($toURL));
            }
        } else {


            $itrial_member = $this->Member->getMember($memberData['id']);
            if ($itrial_member['result'])
                $member = $itrial_member['result'][0];
            $member['photo'] = imore_avatar($member['photo']);

            $toURL = $this->set_toURL();

            $layout['profession'] = $this->Lookup->getLookup('profession');
            $layout['profession'] = $layout['profession']['result'];
            $layout['body'] = $this->Lookup->getLookup('body');
            $layout['body'] = $layout['body']['result'];
            $layout['face_skin'] = $this->Lookup->getLookup('face_skin');
            $layout['face_skin'] = $layout['face_skin']['result'];
            $layout['body_skin'] = $this->Lookup->getLookup('body_skin');
            $layout['body_skin'] = $layout['body_skin']['result'];
            $layout['hair'] = $this->Lookup->getLookup('hair');
            $layout['hair'] = $layout['hair']['result'];
            $layout['selfurl'] = $this->selfurl;

            $layout['brand'] = $this->Lookup->getLookup('brand');
            $layout['brand'] = $layout['brand']['result'];
            $layout['question'] = $this->Lookup->getLookup('question');
            $layout['question'] = $layout['question']['result'];
            $layout['interest'] = $this->Lookup->getLookup('interest');
            $layout['interest'] = $layout['interest']['result'];
            $layout['selfurl'] = $this->selfurl . '?action=regExpert';
            $brand_key = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

            $brandarr = array();
            if ($layout['brand']) {
                foreach ($layout['brand'] as $value) {
                    $brandarr[$value['id']] = $value['name'];
                }
            }
            $layout['brand'] = $this->assort_brand($brand_key, $brandarr);

            $layout['member'] = array();

            $itrial_member_condition = $this->Member->getMemberCondition($memberData['id']);
            if ($itrial_member_condition['result']) {
                $layout['member'] = $itrial_member_condition['result'][0];
                $layout['member']['interest'] = explode(',', $layout['member']['interest']);
                $layout['member']['question'] = explode(',', $layout['member']['question']);
                $layout['member']['brand'] = explode(',', $layout['member']['brand']);
            }

            $this->tmpl_assign('ERR_FNBUSERNAME', ERR_FNBUSERNAME);
            $this->tmpl_assign('ERR_USERNAME', ERR_USERNAME);
            $this->tmpl_assign('HAS_USERNAME', HAS_USERNAME);
            $this->tmpl_assign('HAS_EMAIL', HAS_EMAIL);
            $this->tmpl_assign('ERR_PWD', ERR_PWD);
            $this->tmpl_assign('ERR_EMAIL', ERR_EMAIL);
            $this->tmpl_assign('ERR_SALUTATION', ERR_SALUTATION);
            $this->tmpl_assign('ERR_PHONE', ERR_PHONE);
            $this->tmpl_assign('ERR_BIRTH', ERR_BIRTH);
            $this->tmpl_assign('ERR_CAPTCHA', ERR_CAPTCHA);
            $this->tmpl_assign('ERR_CPWD', ERR_CPWD);
            $this->tmpl_assign('HAS_OPASSWORD', HAS_OPASSWORD);
            $this->tmpl_assign('OLDPASSWORD', OLDPASSWORD);
            $this->tmpl_assign('CHANGEEMAIL', CHANGEEMAIL);
            $this->tmpl_assign('selfurl', $this->selfurl);
            $this->tmpl_assign('member', $member);
            $this->tmpl_assign('session', $memberData);
            $this->tmpl_assign('CHECKING', CHECKING);
            $this->tmpl_assign('NO_AGREE', NO_AGREE);
            $global_url = $this->param('URL');
            $this->tmpl_assign('global_url', $global_url);

            $this->tmpl_assign('HAS_PROFESSION', HAS_PROFESSION);
            $this->tmpl_assign('HAS_BODY', HAS_BODY);
            $this->tmpl_assign('HAS_FACE_SKIN', HAS_FACE_SKIN);
            $this->tmpl_assign('HAS_BODY_SKIN', HAS_BODY_SKIN);
            $this->tmpl_assign('HAS_HAIR', HAS_HAIR);
            $this->tmpl_assign('HAS_ADDRESS', HAS_ADDRESS);


            $this->tmpl_assign("brand_key", $brand_key);
            $this->tmpl_assign('layout', $layout);
            $this->tmpl_assign('HAS_HOMEPAGE', HAS_HOMEPAGE);
            $this->tmpl_assign('HAS_QUESTION', HAS_QUESTION);
            $this->tmpl_assign('HAS_INTEREST', HAS_INTEREST);
            $this->tmpl_assign('HAS_BRAND', HAS_BRAND);
            $this->tmpl_assign('HAS_INVITE_PIC', HAS_INVITE_PHOTO);
            $this->tmpl_assign('HAS_PHONE', HAS_PHONE);


            return $this->load_tmpl('memberMyProfile.php');
        }
    }

    function action_MyReg() {
        $toURL = $this->selfurl . '?action=MyReg';
        $memberData = $this->is_login($toURL);
        $toURL = $this->set_toURL();

        if ($this->get_q('issubmit')) {

            $toURL = $this->get_toURL();

            $reg_p = $this->get_q('reg_p');

            $reg_p['id'] = $memberData['id'];
            $password = $this->get_q('password');
            $opassword = $this->get_q('opassword');
            $email = $this->get_q('email');
            $photo = $this->get_q('photo');

            if ($reg_p['email_receive'] <> 1)
                $reg_p['email_receive'] = 0;

            if ($photo) {

                $x = $this->get_q('x');
                $y = $this->get_q('y');
                $w = $this->get_q('w');
                $h = $this->get_q('h');

                $avatar_pic = $photo;
                if ($x) {
                    $avatar_pic = cropPhoto($x, $y, $w, $h, 84, 84, $avatar_pic);
                    $avatar_pic = FB_REDIRECT_URI . str_replace($this->param('WWW'), '', $avatar_pic);
                } else {
                    $avatar_pic = $this->param('URL') . substr(PhotoResize($avatar_pic, 84, 84), 1);
                }
                $reg_p['photo'] = $avatar_pic;
            }

            if ($password || ($email && $email <> $memberData['email'])) {
                if (!$opassword)
                    return showMsgCode(HAS_OPASSWORD, $this->selfurl . '?action=MyReg');
            }

            if ($password)
                $reg_p['password'] = $password;
            if ($opassword) {
                $reg_p['opassword'] = $opassword;
                $reg_p['username'] = $memberData['username'];
            }
            if ($email && $email <> $memberData['email'])
                $reg_p['email'] = $email;

            $reg_p['salutation'] = $reg_p['sex'];
            $postData = $this->Member->postData($reg_p);

            $url = IMOREDOMAIN . "memberApi.php?action=myRegister";


            $memberJsonData = json_decode($this->Member->go_curl($url, $postData), 1);

            if ($memberJsonData['ret'] == 1) {
                $post_data = $reg_p;
                unset($post_data["opassword"]);
                unset($post_data["password"]);

                if ($email && $email <> $memberData['email']) {
                    $post_data['email'] = $email;
                    $post_data['email_status'] = 0;
                    $this->resend_email($email);
                }
                if ($photo)
                    $post_data['photo'] = $memberJsonData['memberData']['photo'];

                $condition = "id=" . mysql_escape_string($memberData['id']);

                db_update('member_imore', $post_data, $condition);



                $this->Member->set_newSession($memberData['id']);


                $this->redirect($this->selfurl . "?action=regTrial&toURL=" . urlencode($toURL));
            }else {
                return showMsgCode($memberJsonData['msg'], $this->selfurl . '?action=MyReg&toURL=' . urlencode($toURL));
            }
        } else {

            $member_data = $this->Member->getMemberCondition($memberData['id']);
            $data = array();
            if ($member_data['result'])
                $data = $member_data['result'][0];
            if ($data['address'] && $data['homepage']) {
                $this->redirect($this->selfurl . "?action=MyProfile&toURL=" . urlencode($toURL));
            }

            $itrial_member = $this->Member->getMember($memberData['id']);
            
            if ($itrial_member['result'])
                $member = $itrial_member['result'][0];
            $member['photo'] = imore_avatar($member['photo']);

            $this->tmpl_assign('ERR_FNBUSERNAME', ERR_FNBUSERNAME);
            $this->tmpl_assign('ERR_USERNAME', ERR_USERNAME);
            $this->tmpl_assign('HAS_USERNAME', HAS_USERNAME);
            $this->tmpl_assign('HAS_EMAIL', HAS_EMAIL);
            $this->tmpl_assign('ERR_PWD', ERR_PWD);
            $this->tmpl_assign('ERR_EMAIL', ERR_EMAIL);
            $this->tmpl_assign('ERR_SALUTATION', ERR_SALUTATION);
            $this->tmpl_assign('ERR_PHONE', ERR_PHONE);
            $this->tmpl_assign('ERR_BIRTH', ERR_BIRTH);
            $this->tmpl_assign('ERR_CAPTCHA', ERR_CAPTCHA);
            $this->tmpl_assign('ERR_CPWD', ERR_CPWD);
            $this->tmpl_assign('HAS_PHONE', HAS_PHONE);

            $this->tmpl_assign('HAS_OPASSWORD', HAS_OPASSWORD);
            $this->tmpl_assign('OLDPASSWORD', OLDPASSWORD);
            $this->tmpl_assign('CHANGEEMAIL', CHANGEEMAIL);
            $this->tmpl_assign('selfurl', $this->selfurl);
            $this->tmpl_assign('member', $member);
            $this->tmpl_assign('session', $memberData);
            $this->tmpl_assign('CHECKING', CHECKING);
            $this->tmpl_assign('NO_AGREE', NO_AGREE);
            $global_url = $this->param('URL');
            $this->tmpl_assign('global_url', $global_url);


            return $this->load_tmpl('memberMyReg.php');
        }
    }

    function action_ajax_login_imore() {
        $email = $this->get_q('email', '');
        $password = $this->get_q('password', '');

        $ret = array();

        $url = IMOREDOMAIN . "memberApi.php?action=ajax_login&username=" . urlencode($email) . "&password=" . urlencode($this->aes->encrypt($password));

        $memberData = json_decode($this->Member->go_curl($url), 1);

        if ($memberData['ret']) {
            $ret['ret'] = 1;
            $ret['msg'] = $memberData['msg'];

            echo json_encode($ret);
            exit();

            //return showMsgCode($memberData['msg'],$this->selfurl.'?action=login');
        } else {

            $this->Member->login_succeed($memberData);

            if ($password <> $this->super_password)
                $this->Points->points_record($memberData['memberInfo']['id'], 'login', "");

            $ret['ret'] = 0;
            $ret['msg'] = LOGINSUCCEED;

            echo json_encode($ret);
            exit();
        }
    }

    function action_ajax_login_fnb() {
        $this->userobj->set('memberData', '');
        $email = $this->get_q('email', '');
        $password = $this->get_q('password', '');
        $ret = array();

        $url = IMOREDOMAIN . "memberApi.php?action=ajax_fnb_login&username=" . urlencode($email) . "&password=" . urlencode($this->aes->encrypt($password));

        $memberData = json_decode($this->Member->go_curl($url), 1);

        if ($memberData['ret']) {
            $ret['ret'] = 1;
            $ret['msg'] = $memberData['msg'];

            echo json_encode($ret);
            exit();
        } else {

            $this->Member->login_succeed($memberData);

            $this->Points->points_record($memberData['memberInfo']['id'], 'login', "");

            $ret['ret'] = 0;
            $ret['msg'] = LOGINSUCCEED;

            echo json_encode($ret);
            exit();
        }
    }

    function resend_email($email = "") {
        $global_url = $this->param('URL');
        $memberData = $this->s_param('memberData');
        $this->Member->resend_email($email, $memberData, $global_url);
    }

    function is_login($toURL) {
        $memberData = $this->s_param('memberData');
        if (!$memberData) {
            $this->userobj->set('toURL', $toURL);
            return showMsgCode(HAS_LOGIN, $this->selfurl . '?action=login');
        }
        return $memberData;
    }

    function set_toURL() {
        $toURLnext_q = $this->get_q('toURL');
        if ($toURLnext_q) {
            $this->userobj->set('toURL', $toURLnext_q);
        }
        $toURLnext = !empty($toURLnext_q) ? $toURLnext_q : '/';
        return $toURLnext;
    }

    function get_toURL() {

        if ($this->s_param('toURL')) {
            $toURL = $this->s_param('toURL');
            $this->userobj->set('toURL', "");
        }
        $toURL = !empty($toURL) ? $toURL : '/';
        return $toURL;
    }

}

header("Content-type: text/html; charset=utf-8");
$webapp = new Member();
$webapp->run();
?>